package com.xiaoyu.tool.service.admin;

import com.xiaoyu.tool.entity.adminUser.AdminUser;
import com.xiaoyu.tool.repository.user.AdminUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

@Service
public class AdminUserService {

    @Autowired
    private AdminUserRepository adminUserRepository;

    public Optional<AdminUser> findAdminUserById(Long uuid) {
        return adminUserRepository.findById(uuid);
    }

    public Boolean checkAdminUsername(String username){
        return !StringUtils.isEmpty(adminUserRepository.findByUsername(username));
    }

    public Optional<AdminUser>login(AdminUser adminUser) {
        String username = adminUser.getUsername();
        String password = adminUser.getPassword();
        return Optional.ofNullable(adminUserRepository.findByUsernameAndPassword(username,password));
    }

    synchronized public void createAdminUser(AdminUser adminUser) {
        adminUserRepository.save(adminUser);
    }

    synchronized public void changeStatus(Long id) {
        AdminUser adminUser = adminUserRepository.findById(id).get();
        adminUser.setStatus(!adminUser.getStatus());
        adminUserRepository.save(adminUser);
    }

    synchronized public void deleteAdminUser(Long id) {
        adminUserRepository.deleteById(id);
    }

    synchronized public void updateAdminUser(Long id, String content) {
        AdminUser adminUser = adminUserRepository.findById(id).get();
        adminUser.setPassword(content);
        adminUserRepository.save(adminUser);

    }
}
